package com.process.servlet;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.base.attachment.AttachmentConstDef;
import com.base.attachment.entity.Attachment;
import com.base.attachment.service.impl.AttachmentService;
import com.base.commons.SpringBeanHolder;
import com.base.security.SecurityUserHolder;

public class SaveProcessServlet extends HttpServlet {
	// 附件最大长度，单位M
	private String maxFileSize;
	// 上传时存在内存中的数据量大小 ，单位M
	private String thresholdSize;
	// 临时文件所放的路径
	private String repositoryPath;

	private AttachmentService attachmentServcie;
	// 发布流程传递参数
	private String pdfName;
	private String xmlFile;
	private String beanId;
	// 预案流程级别
	private String level;

	// Initialize global variables
	public void init() throws ServletException {
		attachmentServcie = SpringBeanHolder.getBean("attachmentSrv");

	}

	// Process the HTTP Get request
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	// Process the HTTP Post request
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		pdfName = request.getParameter("pdfName");
		xmlFile = URLDecoder.decode(URLDecoder.decode(request
				.getParameter("xmlFile")));

		beanId = request.getParameter("beanId");
		level = URLDecoder.decode(URLDecoder.decode(request
				.getParameter("level")));
		String beanClass = "com.eop.entity.Schnario";
		String attachmentType = "schFlow";

		DiskFileItemFactory factory = new DiskFileItemFactory();
		int memorySize = AttachmentConstDef.ATTACHMENT_THRESHOLD_SIZE;
		if (thresholdSize != null && !"".equals(thresholdSize))
			memorySize = Integer.parseInt(thresholdSize) * 1024 * 1024;
		factory.setSizeThreshold(memorySize);
		String tempPath = AttachmentConstDef.ATTACHMENT_REPOSITORY_PATH;
		if (repositoryPath != null && !"".equals(repositoryPath))
			tempPath = repositoryPath;
		factory.setRepository(new File(tempPath));
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		long max = AttachmentConstDef.ATTACHMENT_MAX_FILE_SIZE;
		if (maxFileSize != null && !"".equals(maxFileSize))
			max = Long.parseLong(maxFileSize) * 1024 * 1024;
		upload.setSizeMax(max);

		// if (ServletFileUpload.isMultipartContent(request)) {
		// List<File> files = new ArrayList<File>();
		// File file;
		try {

			Attachment attachment = new Attachment();
			// 如果没有使用springsecurity，则不存储上传人信息，如果使用了springsecurity，用springsecurityholder得到用户。
			if (SpringBeanHolder.getBean("authenticationManager") != null) {
				attachment.setAuthor(SecurityUserHolder
						.getCurrentUserFullName());
			}
			attachment.setLevel(level);
			attachment.setBeanClass(beanClass);
			attachment.setBeanId(beanId);
			attachment.setCreateTime(new Date());
			attachment.setFileName(pdfName+".xml");
			attachment.setFileExt("xml");
			attachment.setLength(0);
			if (attachmentType == null || "".equals(attachmentType)
					|| "null".equalsIgnoreCase(attachmentType)) {
			} else {
				attachment.setAttachmentType(attachmentType);
			}

			attachmentServcie.createAttachment(attachment, xmlFile);

		} catch (Exception e) {
			e.printStackTrace();
		}
		// }

	}

	// Clean up resources
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
